package jian.hamster.game
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import jian.hamster.controller.GameController;
	import jian.hamster.event.GameEvent;
	
	/**
	 * 洞, 洞, 洞, 洞次大次洞次大次
	 * 2014-04-10
	 * @author Jian
	 * @date 2014-04-10
	 */	
	public class Hole extends Sprite
	{
		private static const DIRECTION_LEFT:int=-1;
		private static const DIRECTION_RIGHT:int=1;
		private static const DIRECTION_NONE:int=0;
		private var _originX:int;
		private var _originY:int;
		private static const OFFSET:int = 10;
		
		private var _direction:int;
		
		public function Hole($x:int, $y:int)
		{
			super();
			x = _originX = $x;
			y = _originY = $y;
			this.graphics.beginFill(0xA3A3A3,0.8);
			this.graphics.drawCircle(0,0,40);
			this.graphics.endFill();
			this.buttonMode=true;
			_direction = DIRECTION_NONE;
			GameController.instance.addEventListener(GameEvent.ENTER_FRAME, __onGameEnterFrame);
		}
		
		protected function __onGameEnterFrame(event:Event):void
		{
			switch(_direction)
			{
				case DIRECTION_NONE:
					
					break;
				case DIRECTION_LEFT:
					if(x <= _originX - OFFSET)
					{
						_direction = DIRECTION_RIGHT;
					}
					else
					{
						x-=1;
					}
					break;
				case DIRECTION_RIGHT:
					if(x >= _originX + OFFSET)
					{
						_direction = DIRECTION_LEFT;
					}
					else
					{
						x+=1;
					}
					break;
				default:
					break;
			}
		}
		
		public function move():void
		{
			_direction = Math.random() * 10 > 5 ? DIRECTION_LEFT : DIRECTION_RIGHT;		
		}
		
	}
	
	
}